//function ordenarList2(list) {
//
//	var aux = list[0], insert = false, newList = Array(), contNewlist = 0, cont = list.length, index = 0;
//
//	while (cont) {
//
//		for (var i = 0; i < cont; i++) {
//
//			if (list[i].number === 0) {
//
//				deletedPos(list, i);
//
//				cont--;
//
//				continue;
//			}
//
//
//			if (aux.number > list[i].number || aux.number === 0) {
//
//				insert = true;
//
//				aux = list[i];
//
//				index = i;
//			}
//		}
//		if (insert) {
//
//			deletedPos(list, index);
//
//			cont--;
//
//			index = 0;
//
//			newList[ contNewlist++ ] = aux;
//
//			insert = false;
//
//		}
//		else
//			break;
//	}
//	for (var i = 0; i < list.length; i++) {
//
//		newList[ contNewlist++ ] = list[i];
//	}
//	return newList;
//}

function ordenarList(list) {

	var aux = null;

	var newList = Array();

	var i = 0, change = false;

	while (list.length !== 0) {

		aux = list[0];

		for (var j = 0; j < list.length; j++) {

			if (aux.number > list[j].number) {
				aux = list[j];
				control = j;
				change = true;
			}
		}
		if (i > 0 && newList[i - 1].number === aux.number) {

			newList[i - 1] = aux;
		}
		else
			newList[i++] = aux;

		if (change) {
			list = deletedPos(list, control);
			change = false;
		}
		else
			list = deletedPos(list, 0);

	}

	for (var index = 0, index2 = newList.length; i < list.length; index++) {

		newList[ index2++ ] = list[index];
	}
	return newList;

}
function deletedPos(vector, index) {

	var newVector = Array();

	for (var i = 0, j = 0; i < vector.length; i++) {
		if (i !== index)
			newVector[j++] = vector[i];
	}

	return newVector;

}

function getAbsoluteElementPosition(element) {

	if (typeof element === "string")
		element = document.getElementById(element)

	if (!element)
		return {top: 0, left: 0};

	var y = 0;
	var x = 0;
	while (element.offsetParent) {
		x += element.offsetLeft;
		y += element.offsetTop;
		element = element.offsetParent;
	}
	return {top: y, left: x};
}
function findAbsolutePosition(obj) {
	var curleft = 0, curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}

	return {top: curtop, left: curleft};
}
function getElementList(coord, list) {

	var aux = list.length, borrar = false, coordYS = 0, coordXS;

	for (var i = 0; i < aux; i++) {

//		if( list[i].class === 'globisChange17')		
////
		//console.log((list[i].class + " coord = " + coord.x + " >= " + list[i].Xi + " && " + coord.y + " >= " + list[i].Yi) + " && " + (coord.x + " <= " + list[i].Xf + " && " + coord.y + " <= " + list[i].Yf));
////			

		coordYS = coord.y + scrollTop;

		coordXS = coord.x + scrollLeft;

		if ((coordXS >= list[i].Xi && coordYS >= list[i].Yi) && (coordXS <= list[i].Xf && coordYS <= list[i].Yf)) {

			console.log((list[i].class + " coord = " + coordXS + " >= " + list[i].Xi + " && " + coordYS + " >= " + list[i].Yi) + " && " + (coordXS + " <= " + list[i].Xf + " && " + coordYS + " <= " + list[i].Yf));

			if (elementAux) {

				if (elementAux.class !== list[i].class) {

					//console.log( elementAux.Xi +' <= '+ list[i].Xi +' && '+ elementAux.Yi +' <= '+ list[i].Yi  +' && '+ elementAux.Xf +' >= '+ list[i].Xf +' && '+ elementAux.Yf +' >= '+ list[i].Yf);

					//if( ( elementAux.Xi <= list[i].Xi && elementAux.Yi <= list[i].Yi ) && ( elementAux.Xf >= list[i].Xf && elementAux.Yf >= list[i].Yf)){

					elementAux = list[i];

					if (prevElement) {

						$jquery('.' + prevElement.class).css('border', '');
					}
					prevElement = list[i];
				}

			}
			else
				elementAux = list[i];

			break;
		}
	}

	if (elementAux) {

		$jquery('.' + elementAux.class).css('border', '4px solid rgb(0, 0, 0)');

		prevElement = elementAux;
	}

	websocketClient.penduloTrue();
}

function elementPos(element) {


	var leftReal = 0, topReal = 0, insertElement = false;

	//listElement[cont++] = element;

	//if ($jquery(element).contents().toArray().length) {

	//	childrens = true;

	//$jquery(element).find('*').each(function(i, element2) {

	if ($jquery(element).children().toArray().length) {

		var firstChid = $jquery(element).children().get(0);

		var posChild = findAbsolutePosition(firstChid), posPatern = findAbsolutePosition(element);

		if ((parseFloat(posChild.top) !== parseFloat(posPatern.top)) || (parseFloat(posChild.left) !== parseFloat(posPatern.left))) {

			$jquery(element).addClass('globisChange' + cont);

			insertElement = true;

			leftReal = parseFloat($jquery(element).css('margin-left')) + parseFloat($jquery(element).css('border-left')) + parseFloat($jquery(element).css('padding-left'));

			topReal = parseFloat($jquery(element).css('margin-top')) + parseFloat($jquery(element).css('border-top')) + parseFloat($jquery(element).css('padding-top'));

		}
	}
	else if (!$jquery(element).children().toArray().length) {

		$jquery(element).addClass('globisChange' + cont);

		insertElement = true;

		pos = findAbsolutePosition(element);

		leftReal = parseFloat($jquery(element).css('margin-left')) + parseFloat($jquery(element).css('border-left')) + parseFloat($jquery(element).css('padding-left'));

		topReal = parseFloat($jquery(element).css('margin-top')) + parseFloat($jquery(element).css('border-top')) + parseFloat($jquery(element).css('padding-top'));
	}
	if ($jquery(element).css('position') === 'fixed' && ($jquery(element).position().left || $jquery(element).position().top)) {
		// posicionar el left y el top con respecto a m, b, p  en el lef y top 
		leftReal += parseFloat($jquery(element).position().left);

		topReal += parseFloat($jquery(element).position().top);

		insertElement = true;
	}
	else {

		leftReal += parseFloat(findAbsolutePosition(element).left);

		topReal += parseFloat(findAbsolutePosition(element).top);

		insertElement = true;
	}

	if (insertElement) {

		var object = {
			class: 'globisChange' + cont++,
			Xi: leftReal + 8,
			Yi: topReal + 8,
			Xf: leftReal + parseFloat($jquery(element).width()) + parseFloat($jquery(element).css('margin-bottom')) + parseFloat($jquery(element).css('border-bottom')) + parseFloat($jquery(element).css('padding-bottom')) + 8,
			Yf: topReal + parseFloat($jquery(element).height()) + parseFloat($jquery(element).css('margin-right')) + parseFloat($jquery(element).css('border-right')) + parseFloat($jquery(element).css('padding-right')) + 8,
			//number: parseFloat(leftReal) * parseFloat(topReal)
		};
		//if(cont == 9 || cont == 8)alert('sdf');

		return object;
	}

	return false;
}

function setChilds(element) {

	var obj = elementPos(element, contList);

	if ($jquery(element).contents().toArray().length) {

		$jquery(element).contents().find('*').each(function(i) {

			if ($jquery(this).children().toArray().length)
				setChilds(this);

		});
	}
	if (obj) {

		listElement[contList++] = obj;
	}


}

function hasClass() {
console.log('start drag');
	var contList = listElement.length;
	
    resetAll();
	  
	parse();
	
	for (var i = 0; i < contList; i++) {
		//console.log(listElement[i]);
		if (!$jquery('#pageLoad').find('*').hasClass(listElement[i].class))
			listElement.splice(i, 1);
	}
	
}

function insertElement(elementToInsert, elementToReplace) {

    $jquery(elementToInsert).removeAttr('id');

	$jquery(elementToReplace).each(function() {
		
		$jquery.each(this.attributes, function() {
            
		   if( this.name !== 'href' && this.name !== 'src')	
			$jquery(elementToInsert).attr(this.name, this.value);

		});
	});
	//if( !$jquery(elementToInsert).css('width') )
		$jquery(elementToInsert).css('width','100');
	
    //if( !$jquery(elementToInsert).css('height') )
		$jquery(elementToInsert).css('height','100');
	
	$jquery(elementToInsert).insertAfter(elementToReplace);
	$jquery(elementToReplace).remove();
	//$jquery(elementToReplace).replaceWith(elementToInsert);

}
var globisIndex = -1, aToken = "", elementAux = null;


